package com.neuedu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.pojo.AdminTable;
import com.neuedu.pojo.CadreTable;
import com.neuedu.pojo.Result;
import com.neuedu.service.AdminTableService;
import com.neuedu.mapper.AdminTableMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* @author Administrator
* @description 针对表【admin_table(管理员信息表)】的数据库操作Service实现
* @createDate 2025-06-17 09:04:08
*/
@Service
public class AdminTableServiceImpl extends ServiceImpl<AdminTableMapper, AdminTable>
    implements AdminTableService{
    @Autowired
    private AdminTableMapper adminTableMapper;
    @Override
    public Result<AdminTable> getAdminTableById(Integer id) {
        AdminTable adminTable = adminTableMapper.selectById(id);
        if (adminTable !=null){
            return Result.success(adminTable);
        }
        return Result.fail();
    }

    @Override
    public Result<List<AdminTable>> getAdminTables(Integer currentPage, Integer pageSize, String phone,String adminName) {
        // 设置条件
        Page<AdminTable> page = new Page<>(currentPage,pageSize);
        // 设置条件查询
        LambdaQueryWrapper<AdminTable> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(adminName),AdminTable::getName,adminName)
                .like(StringUtils.isNotEmpty(phone),AdminTable::getPhone,phone);
        // 查询
        this.adminTableMapper.selectPage(page,wrapper);
        if (page.getRecords().size()>0){
            return Result.success(page.getRecords(),page.getTotal());
        }
        return Result.fail();
    }

    @Override
    public Result<Void> addAdminTable(AdminTable adminTable) {
        // 需要针对表中需要的数据，但是页面添加的时候没有提交的在这里补充
        adminTable.setIsDelete(0);
        adminTable.setCreateTime(new Date());
        adminTable.setUpdateTime(new Date());
        // 添加
        int insert = this.adminTableMapper.insert(adminTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> updateAdminTable(AdminTable adminTable) {
        // 需要针对表中需要的数据，但是页面添加的时候没有提交的在这里补充
        // adminTable.setIsDelete(0);
        // adminTable.setCreateTime(new Date());
        adminTable.setUpdateTime(new Date());
        // 添加
        int insert = this.adminTableMapper.updateById(adminTable);
        if (insert > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<Void> deleteAdminTable(Integer id) {
        //AdminTable adminTable = new AdminTable();
        //adminTable.setIsDelete(1);
        //adminTable.setCadreId(id);
        // 修改
        //int insert = this.adminTableMapper.updateById(adminTable);
        LambdaUpdateWrapper<AdminTable> wrapper = new LambdaUpdateWrapper<>();
        wrapper.set(AdminTable::getIsDelete,1)
                .eq(AdminTable::getCadreId,id);
        int update = this.adminTableMapper.update(wrapper);
        if (update > 0){
            return Result.success();
        }
        return Result.fail();
    }

    @Override
    public Result<AdminTable> loginAdmin(String number, String password) {
        // 2. 根据number查询数据库
        AdminTable admin=adminTableMapper.selectByName(number);
        if (admin == null) {
            return Result.fail("用户不存在");
        }
        // 3. 比较密码和手机号
        if (password.equals(admin.getPassword())) {
            return Result.success(admin,"登录成功");
        } else {
            return Result.fail("密码错误");
        }
    }
}




